<?php
/**
 * Name:U数据输出类
 * Version:1.0
 * Author:阳光男孩
 * Author QQ:51154393
 * Author Url:www.everif.com
 **/

namespace Ue\tools;
class out
{

    public $data;//数据
    public $conf;//配置信息

    public $app = null;//APP应用
    public $ver = null;//APP应用版本

    public $key = null; // aes密钥

    public $encryption = false;
    public function __construct()
    {
        $this->conf = c('app');//获取配置信息
    }

    public function data($data)
    {//添加输出数据
        $this->data = $data;
        return $this;
    }

    public function go($code, $msg = null)
    {//输出
        if (!$msg && !is_array($msg)) {
            $lang_msg = c('msg');//返回数组
            $msg = $lang_msg[$code];
        }
        $data = ['code' => $code, 'msg' => $msg, 'time' => time()];
        if ($this->conf['API_OUT_TYPE'] == 'json') {
            if(is_array($this->data)){$data['data'] = json_encode($this->data);}elseif(!empty($this->data)){$data['data'] = $this->data;}
            // 判断数据是否加密返回
            if ($this->app && $this->app['api_encrypt'] == 'on' && !empty($this->data)){
                if (!isset($this->key) or !isset($this->app['api_key'])){
                    $data['code'] = 201;
                    $data['msg'] = '加密密钥配置有误';
                }else{
                    // 使用aes加密
                    $Aes = t('Aes', $this->key);
                    $data['data'] = $Aes->encrypt($data['data']);
                    // 使用rsa加签
                    $keyConfig = json_decode($this->app['api_key'], true);
                    $Rsa = t('Rsa');
                    $data['sign'] = $Rsa->rsaSign($data['data'],$keyConfig['private']);
                }

            }

            if ($this->conf['API_RUN_COST'] == 'on') {//计算运行成本
                $cost = runCost();
                $data['run'] = ['ms' => $cost[0], 'ram' => $cost[1] . 'k'];
            }
            $echo_data = json_encode($data);
        } elseif ($this->conf['API_OUT_TYPE'] == 'xml') {
            header("Content-type:text/xml");//输出xml头信息
            $xml = t('Xml');//实例化类
            if(is_array($this->data)){$data['data'] = $xml->toXml($this->data);}elseif(!empty($this->data)){$data['data'] = $this->data;}
            // 判断数据是否加密返回
            if ($this->app && $this->app['api_encrypt'] == 'on' && !empty($this->data)){
                // 加密
                if (!isset($this->app['api_key'])){
                    $data['code'] = 201;
                    $data['msg'] = '加密密钥配置有误';
                }else{
                    $keyConfig = json_decode($this->app['api_key'], true);
                    $Rsa = t('Rsa');
                    $data['data'] = $Rsa->privateEncrypt($data['data'],$keyConfig['private']);
                }
            }
            if ($this->conf['API_RUN_COST'] == 'on') {//计算运行成本
                $cost = runCost();
                $data['run'] = ['ms' => $cost[0] . 'ms', 'ram' => $cost[1] . 'k'];
            }
            $echo_data = $xml->toXml($data);//转为数组
        } else {
            $echo_data = $msg;
        }
        echo $echo_data;
        exit;
    }

    public function checkSign($data, $app_key)
    {
        if ($this->conf['API_SIGN'] == 'on') {//验证签名
            if (!isset($data['sign']) || empty($data['sign'])) return false;
            $sign = strtolower($data['sign']);//获取签名
            $s = $this->arr_Sign($data, true, $app_key);//生成签名
            if ($s != $sign) return false;//签名有误
        }
        return true;
    }

    public function checkTime($time)
    {
        if ($this->conf['API_DATA_EXP'] > 0) {//验证时间
            $t = time() - intval($time);//服务器时间-客户端时间，对比时间差
            if ($t > $this->conf['API_DATA_EXP']) return false;//客户端时间小于服务器时间
        }
        return true;
    }

    public function txt_Arr($txt)
    {//文本转数组
        $arr = explode('&', $txt);
        $array = [];
        foreach ($arr as $value) {
            $tmp_arr = explode('=', $value);
            if (is_array($tmp_arr) && count($tmp_arr) == 2) {
                $array = array_merge($array, [$tmp_arr[0] => $tmp_arr[1]]);
            }
        }
        return $array;
    }

    public function arr_Sign($arr, $md5 = true, $app_key = '')
    {//数组签名
        unset($arr['sign']);
        $sign = '';
        foreach ($arr as $k => $v) {
            $sign = $sign . $k . '=' . $v . '&';
        }
        $sign = rtrim($sign, '&') . $app_key;
        if ($md5) {
            return md5($sign);
        } else {
            return $sign;
        }
    }

    public function setVer($v,$apiname){
        $this->ver = $v;
        $ApiWhiteList = explode(",",$this->conf['API_WHITE']);//白名单返回数组
        $this->encryption = !in_array($apiname,$ApiWhiteList)?true:false;//是否加密
        return $this;
    }
    public function setApp($app,$apiname){
        $this->app = $app;
        $ApiWhiteList = explode(",",$this->conf['API_WHITE']);//白名单返回数组
        $this->encryption = !in_array($apiname,$ApiWhiteList)?true:false;//是否加密
        return $this;
    }
}
